/**
 * chenuu.com
 * 
 * @author  zhaoxu.com@gmail.com
 * @created 2012-11-3
 */
package com.chenuu.integration.httpclient.dropbox;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.chenuu.integration.httpclient.ChenuuHttpClient;
import com.chenuu.integration.httpclient.RequestParameter;
import com.chenuu.integration.httpclient.oauth.OAuthToken;
import com.chenuu.integration.httpclient.utils.OAuthUtil;
import com.chenuu.integration.httpclient.utils.RequestUtil;
import com.chenuu.integration.httpclient.utils.ResponseUtil;

/**
 *
 */
public class DropboxClientImpl extends ChenuuHttpClient implements DropboxClient {

	private static final String REQUEST_TOKEN_URL = "https://api.dropbox.com/1/oauth/request_token";
	private static final String AUTHORIZE_URL = "https://www.dropbox.com/1/oauth/authorize";
	private static final String ACCESS_TOKEN_URL = "https://api.dropbox.com/1/oauth/access_token";

	private static final String APP_KEY = "mv96yaa3f16827u";
	private static final String APP_SECRET = "39jn981fnp7csso";

	private static final String REQUEST_METHOD = "POST";
	private static final String SIGNATURE_METHOD = "HMAC-SHA1";

	/*
	 * (non-Javadoc)
	 * @see com.chenuu.integration.httpclient.dropbox.DropboxClient#fetchRequestToken()
	 */
	public OAuthToken fetchRequestToken() {

		String timestamp = OAuthUtil.generateTimestamp();
		String nonce = OAuthUtil.generateNonce();

		List<RequestParameter> params = new ArrayList<RequestParameter>();
		params.add(new RequestParameter("oauth_consumer_key", APP_KEY));
		params.add(new RequestParameter("oauth_signature_method", SIGNATURE_METHOD));
		params.add(new RequestParameter("oauth_timestamp", timestamp));
		params.add(new RequestParameter("oauth_nonce", nonce));

		String signatureBaseString = OAuthUtil.generateSignatureBaseString(REQUEST_METHOD,
				REQUEST_TOKEN_URL, params);

		String signature = RequestUtil.signature(signatureBaseString, APP_SECRET + "&",
				SIGNATURE_METHOD);
		params.add(new RequestParameter("oauth_signature", signature));

		String requestUrl = RequestUtil.buildURI(REQUEST_TOKEN_URL, params);
		String response = this.post(requestUrl);

		Map<String, String> responseMap = ResponseUtil.getResponseMap(response);

		String token = responseMap.get("oauth_token");
		String tokenSecret = responseMap.get("oauth_token_secret");

		OAuthToken oauthToken = new OAuthToken(token, tokenSecret);

		return oauthToken;
	}

	/*
	 * (non-Javadoc)
	 * @see com.chenuu.integration.httpclient.dropbox.DropboxClient#buildAuthorizeURL(com.chenuu.integration.httpclient.oauth.OAuthToken)
	 */
	public String buildAuthorizeURL(OAuthToken oauthToken) {
		return AUTHORIZE_URL + "?oauth_token=" + oauthToken.getKey()
				+ "&oauth_callback=http://chenuu.com?ttt";
	}

	/*
	 * (non-Javadoc)
	 * @see com.chenuu.integration.httpclient.dropbox.DropboxClient#fetchAccessToken(com.chenuu.integration.httpclient.oauth.OAuthToken)
	 */
	public OAuthToken fetchAccessToken(OAuthToken oauthToken) {

		String timestamp = OAuthUtil.generateTimestamp();
		String nonce = OAuthUtil.generateNonce();

		List<RequestParameter> params = new ArrayList<RequestParameter>();
		params.add(new RequestParameter("oauth_consumer_key", APP_KEY));
		params.add(new RequestParameter("oauth_signature_method", SIGNATURE_METHOD));
		params.add(new RequestParameter("oauth_timestamp", timestamp));
		params.add(new RequestParameter("oauth_nonce", nonce));
		params.add(new RequestParameter("oauth_token", oauthToken.getKey()));

		String signatureBaseString = OAuthUtil.generateSignatureBaseString(REQUEST_METHOD,
				ACCESS_TOKEN_URL, params);

		String signature = RequestUtil.signature(signatureBaseString,
				APP_SECRET + "&" + oauthToken.getSecret(), SIGNATURE_METHOD);
		params.add(new RequestParameter("oauth_signature", signature));

		String requestUrl = RequestUtil.buildURI(ACCESS_TOKEN_URL, params);
		String response = this.post(requestUrl);

		Map<String, String> responseMap = ResponseUtil.getResponseMap(response);

		String token = responseMap.get("oauth_token");
		String tokenSecret = responseMap.get("oauth_token_secret");

		oauthToken.setKey(token);
		oauthToken.setSecret(tokenSecret);

		return oauthToken;
	}

	public static void main(String[] args) {
		DropboxClientImpl client = new DropboxClientImpl();

		// RequestToken oauthToken = oauth.fetchRequestToken();
		// System.out.println(oauthToken.getToken());
		// System.out.println(oauthToken.getTokenSecret());
		//
		// System.out.println(oauth.buildAuthorizeURL(oauthToken));

		// http://chenuu.com/?ttt&uid=206536&oauth_token=8egllswpqf5kkpn

		// OAuthToken oauthToken = new OAuthToken();
		// oauthToken.setToken("8egllswpqf5kkpn");
		// oauthToken.setTokenSecret("3bpm25mp6kmrbno");
		//
		// String res = oauth.fetchAccessToken(oauthToken);
		// System.out.println(res);

		// oauth_token_secret=2pfi6q1ru2k0n9b&oauth_token=0ii7jpj4p6x2wwm&uid=206536

		OAuthToken oauthToken = new OAuthToken("0ii7jpj4p6x2wwm", "2pfi6q1ru2k0n9b");
		// String res = client.fetchAccountInfo(oauthToken);
	}
}
